﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace HCLSMod.NetSocket
{
    public delegate void TCPIPSerRecCommand(object sender, Socket ClientSocket, ref TCPIPCommandBase RecCommand);

    public class TCPIPSerCommand: TCPServerSocket
    {
        public event TCPIPSerRecCommand OnRecCommand;

        public TCPIPSerCommand(int fSerPort) : base(fSerPort)
        {
        }

        public TCPIPSerCommand(IPAddress fSerIPAddress, int fSerPort)
            : base(fSerIPAddress, fSerPort)
        {
        }

        public override void OnRecDataEvent(object sender, Socket ClientSocket, ref byte[] RecData)
        {
            base.OnRecDataEvent(sender, ClientSocket, ref RecData);
            TCPIPCommandBase fCommand = TCPIPCommandBase.DeserializeCommand(RecData);
            RecData = null;
            if (fCommand != null)
            {
                if (OnRecCommand != null)
                    OnRecCommand(this, ClientSocket, ref fCommand);
                if (fCommand != null)
                {
                    if (fCommand.ResponeCode == ResponeStatusCode.None)
                        fCommand.ResponeCode = ResponeStatusCode.ACK;
                    RecData = fCommand.SerializeCommand();
                }
            }
        }
        
    }
}
